package com.example.ktapplication.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.ktapplication.AppGlobals
import com.example.ktapplication.R
import com.example.ktapplication.adapter.SystemListAdapter.OnItemClickListener
import com.example.ktapplication.adapter.SystemListAdapter.SysViewHolder
import com.example.ktapplication.bean.SettingBean
import com.example.ktapplication.bean.Sys
import com.example.ktapplication.databinding.RvSettingItemLayoutBinding
import com.example.ktapplication.enum.FunctionEnum
import com.example.ktapplication.utils.CleanDataUtils

class SettingAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val links by lazy {
        mutableListOf<SettingBean>()
    }

    var itemCallBack:OnItemClickListener? = null

    var  latitude:String=""
    var  longitude:String=""

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.rv_setting_item_layout, parent, false)
        return SettingViewHolder(view)
    }

    @SuppressLint("NotifyDataSetChanged")
    fun addData(data: MutableList<SettingBean>) {
        links.clear()
        links.addAll(data)
        notifyDataSetChanged()
    }

    fun setLocationContent(latitude:String,longitude:String){
        this.latitude=latitude
        this.longitude=longitude
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return links.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is SettingViewHolder) {
            val settingItem = links[position]
            holder.binding.llSettingContent.setOnClickListener(View.OnClickListener {
                itemCallBack?.onItemClick(settingItem)
            })
            holder.binding.tvItmeTitle.text=settingItem.title
            if (settingItem.title.equals("清理")){
                holder.binding.tvItmeCash.visibility=View.VISIBLE
                val cash= CleanDataUtils.getInstance().getTotalCacheSize(AppGlobals.getApplication())
                holder.binding.tvItmeCash.text=cash
            }else{
                holder.binding.tvItmeCash.visibility=View.GONE
            }
            if (settingItem.type==FunctionEnum.LOCATION){
                holder.binding.llLongitudeBody.visibility=View.VISIBLE
                holder.binding.txtLatitude.text=latitude
                holder.binding.txtLongitude.text=longitude
            }else{
                holder.binding.llLongitudeBody.visibility=View.GONE
            }
        }
    }

    inner class SettingViewHolder(item: View) : RecyclerView.ViewHolder(item) {
        val binding by lazy {
           RvSettingItemLayoutBinding.bind(item)
        }
    }
    interface OnItemClickListener {
        fun onItemClick(bean:SettingBean)
    }
}